package cn.hk.dept.service.impl;

import cn.hk.common.utils.DeptTreeUtil;
import cn.hk.dept.domain.Area;
import cn.hk.dept.domain.Dept;
import cn.hk.dept.domain.vo.DeptInfoVo;
import cn.hk.dept.domain.vo.DeptVo;
import cn.hk.dept.mapper.DeptMapper;
import cn.hk.dept.service.AreaService;
import cn.hk.dept.service.DeptService;
import cn.hk.vaccination.service.DeptVaccinesStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author xu
 * @since 2023-03-09
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private DeptVaccinesStockService deptVaccinesStockService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private DeptTreeUtil deptTreeUtil;

    @Autowired
    private DeptService deptService;

    @Autowired
    private RedisCache redisCache;

    @Override
    public List<DeptVo> getDeptList(Area per) {
        List<DeptVo> deptList = deptMapper.selectDeptVoListByAreaCode(per.getAreaCode());
        return deptList;
    }

    @Override
    public List<DeptVo> getDeptVaccinesList(Area per ,  Long vaccinesId) {
        List<DeptVo> deptVos = deptMapper.selectDeptVoListByVid(per.getAreaCode(),vaccinesId);
//        List<DeptVo> deptVoList = CollUtil.newArrayList();
//        try {
//            List<Dept> deptList = deptMapper.selectDeptListByAreaCodeAndDeptTypeAndIsOpen(per.getAreaCode());
//            for (Dept dept : deptList) {
//
//                DeptVo deptVo = new DeptVo();
//                deptVo.setDeptId(dept.getDeptId());
//                deptVo.setDname(dept.getDeptName());
//                deptVo.setAdequate(2);
//                DeptVaccinesStock deptVaccinesStock = deptVaccinesStockService.getDeptVaccinesStockByDeptIdAndVid(dept.getDeptId(), vaccinesId);
//                if (deptVaccinesStock != null) {
//                    deptVo.setVaccinesId(deptVaccinesStock.getVaccinesId().longValue());
//                    deptVo.setAdequate(deptVaccinesStock.getAdequate());
//                }
//                deptVoList.add(deptVo);
//            }
//        } catch (Exception e) {
//            Console.log("发生了异常", e);
//        }
        System.out.println(deptVos);

        return deptVos;
    }

    @Override
    public Dept getOneById(Long deptId) {

        return deptMapper.selectOneById(deptId);
    }



    @Override
    public  List<DeptInfoVo> getDeptByPId(Long deptId) {
        List<DeptInfoVo> deptInfoVoList = deptMapper.selectDeptByPId(deptId);
        return deptInfoVoList;
    }

    @Override
    public List<Area> getAreaList() {
        List<Area> areaList = areaService.getAreaList();
        List<Area> areaTrees = deptTreeUtil.treeDeptBuild(areaList,null);
        return areaTrees;
    }

    @Override
    public Dept getOneByDeptId(Long deptId) {
        return deptMapper.selectOneByDeptId(deptId);
    }



    @Override
    public List<DeptVo> getDeptVaccinesStockList(Long deptId, Long vaccinesId) {
        List<DeptVo> deptVoList = deptMapper.selectDeptVoListByDeptIdAndVid(deptId, vaccinesId);
        for (DeptVo deptVo : deptVoList) {
            if (deptVo.getAdequate()==null){
                deptVo.setAdequate(2);
            }
        }
        return deptVoList;
    }

    @Override
    public List<DeptVo> getListByAreaCode(String areaCode) {
        return deptMapper.selectDeptVoListByAreaCode(areaCode);
    }
}
